 /* Copyright (C)
  * 2024 ZKLX Optoelectronic Technology Co.LTD
  * All rights reserved.
  */
 /**
  * @file main.cpp
  * @brief 
  * @author xuke
  * @version 1.0
  * @date 2024-09-28
  */

#include "/home/xuke/bin/dbg.h"
#include <iostream>
#include <unistd.h>
#include <crow_all.h>
#include "misc_tool.h"

/*--------------------------------extern--------------------------------------------*/

/*--------------------------------globals-------------------------------------------*/

/*--------------------------------locals--------------------------------------------*/

/*--------------------------------funcs---------------------------------------------*/
int exp1_first_application_main()
{
	crow::SimpleApp app;
	//define your endpoint at the root directory
	CROW_ROUTE(app, "/") ([]() {
		return "Hello world";
	});

	//set the port, set the app to run on multiple threads, and run the app
	app.port(18080).multithreaded().run();
	return 0;
}

int exp2_simple_webpage()
{
	crow::SimpleApp app;
	CROW_ROUTE(app, "/") ([](){
		auto page = crow::mustache::load_text("fancypage.html");
		return page;
	});

	app.port(18080).multithreaded().run();
	return 0;
}

int exp3_with_variable()
{
	crow::SimpleApp app;

	//默认页
	CROW_ROUTE(app, "/") ([](){
		auto page = crow::mustache::load("index.html");
		return page.render();
	});

	// 处理图片请求的路由
	CROW_ROUTE(app, "/favicon.ico") ([](const crow::request& req) {
		// 打开图片文件
		std::ifstream file("./templates/favicon.ico", std::ios::binary);
		if (!file) {
			return crow::response(404); // 如果文件不存在，返回404错误
		}

		 // 创建响应对象
		 crow::response res;
		 // 设置内容类型为图片
		 res.set_header("Content-Type", "image/jpng");
		 // 读取文件内容到响应体
		 res.body = std::string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
		 return res;
	});

	// 处理视频请求的路由
	CROW_ROUTE(app, "/ftv_1.mp4") ([](const crow::request& req) {
		// 打开文件
		std::ifstream file("./templates/ftv_1.mp4", std::ios::binary);
		if (!file) {
			return crow::response(404); // 如果文件不存在，返回404错误
		}

		 // 创建响应对象
		 crow::response res;
		 // 设置内容类型为图片
		 res.set_header("Content-Type", "video/mp4");
		 // 读取文件内容到响应体
		 res.body = std::string(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
		 return res;
	});

	//从URL接收一个变量
	CROW_ROUTE(app, "/<string>") ([](std::string var) { //传入参数
		auto page = crow::mustache::load("fancypage.html"); //需要处理变量, 所以用load而不是locad_text
		crow::mustache::context ctx; //创建上下文
		ctx["person"] = misc_url_decode(var); //给html中定义的person变量赋值, 使用url_decode解码URL
		return page.render(ctx); //使用render将上下文应用到模板
	});

	app.port(18080).multithreaded().run();
	return 0;
}

int exp4_cv_web()
{
	crow::SimpleApp app;

	//默认页
	CROW_ROUTE(app, "/") ([]() {
		auto page = crow::mustache::load("index.html");
		return page.render();
	});

	//其他html
	CROW_ROUTE(app, "/<string>") ([](const crow::request& req, std::string htmlFile) {
		auto page = crow::mustache::load(htmlFile);
		return page.render();
	});

	// 处理CSS
	CROW_ROUTE(app, "/css/<string>") ([](const crow::request& req, std::string cssFile) {
		 std::string cssPath = "./templates/css/" + cssFile; // CSS路径
		 std::ifstream t(cssPath);
		 std::string cssCode((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());

		 crow::response res;
		 res.set_header("Content-Type", "text/css");
		 res.body = cssCode;
		 return res;
	});

	// 处理JS
	CROW_ROUTE(app, "/js/<string>") ([](const crow::request& req, std::string jsFile) {
		 std::string jsPath = "./templates/js/" + jsFile; // JS路径
		 std::ifstream t(jsPath);
		 std::string jsCode((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());

		 crow::response res;
		 res.body = jsCode;
		 return res;
	});

	app.port(18080).multithreaded().run();
	return 0;
}

/**
 * @brief main
 * @param argc
 * @param argv[]
 * @return 0
 */
int main(int argc, const char *argv[])
{
	exp4_cv_web();
	return 0;
}

